import Mock from "mockjs";
import {
  adminMenuList,
  managerMenuList,
  tenementList,
  userMenuList,
} from "./data";

/** 配置mock */
Mock.setup({
  timeout: "100-400",
});

/** 登录接口模拟 */
Mock.mock("https://www.demo.com/login", "post", (opt: any) => {
  const { username, password } = JSON.parse(opt.body);

  if (username === "admin" && password === "123321") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        username: "韩立",
        token: "mocktoken123456admin",
      },
    };
  }

  if (username === "admin2" && password === "123321") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        username: "南宫婉",
        token: "mocktoken123456admin2",
      },
    };
  }

  if (username === "user" && password === "123321") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        username: "紫灵",
        token: "mocktoken123456user",
      },
    };
  }

  return {
    code: 401,
    message: "用户名或密码有误",
    data: null,
  };
});

/** 菜单接口模拟 */
Mock.mock("https://www.demo.com/menu", "get", (opt: any) => {
  const token = sessionStorage.getItem("token");

  if (token === "mocktoken123456admin") {
    return {
      code: 200,
      message: "请求成功",
      data: adminMenuList,
    };
  }

  if (token === "mocktoken123456admin2") {
    return {
      code: 200,
      message: "请求成功",
      data: managerMenuList,
    };
  }

  if (token === "mocktoken123456user") {
    return {
      code: 200,
      message: "请求成功",
      data: userMenuList,
    };
  }

  return {
    code: 200,
    message: "请求失败",
    data: [],
  };
});

/** 工作台-能源数据接口 */
Mock.mock("https://www.demo.com/energyData", "get", () => {
  return {
    code: 200,
    message: "请求成功",
    data: {
      legend: ["煤", "气", "油", "电", "热"],
      xAxis: ["0:00", "4:00", "8:00", "12:00", "16:00", "20:00", "24:00"],
      series: [
        { name: "煤", data: [120, 132, 101, 134, 90, 230, 210] },
        { name: "气", data: [220, 182, 191, 234, 290, 330, 310] },
        { name: "油", data: [150, 232, 201, 154, 190, 330, 410] },
        { name: "电", data: [320, 332, 301, 334, 390, 330, 320] },
        { name: "热", data: [820, 932, 901, 934, 1290, 1330, 1320] },
      ],
    },
  };
});

/** 租户列表接口模拟 */
Mock.Random.extend({
  phone: function () {
    var phonePrefixs = ["13", "14", "15", "16", "17", "18", "19"]; // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{9}/); //Number()
  },
});

Mock.mock("https://www.demo.com/userList", "post", (opt: any) => {
  const { pageNo, pageSize } = JSON.parse(opt.body);
  return {
    code: 200,
    message: "请求成功",
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          id: '@string("number", 6)',
          name: "@cname",
          "statue|1": ["1", "2", "3"],
          tel: "@phone",
          "business|1": [
            "制造业",
            "互联网",
            "新媒体",
            "美业",
            "新能源",
            "物流",
            "电商",
          ],
          email: "@email",
          creditCode: '@string("number", 18)',
          industryNum: '@string("number", 15)',
          organizationCode: "@string('upper',9)",
          legalPerson: "@cname",
        },
      ],
      total: 100,
    }),
  };
});

Mock.mock("https://www.demo.com/delUser", "post", (opt: any) => {
  return {
    code: 200,
    message: "请求成功",
    data: "删除成功",
  };
});

Mock.mock("https://www.demo.com/batchDelUser", "post", (opt: any) => {
  return {
    code: 200,
    message: "请求成功",
    data: "删除成功",
  };
});

Mock.mock("https://www.demo.com/editUser", "post", (opt: any) => {
  const { id } = JSON.parse(opt.body);
  console.log(id);
  if (id) {
    return {
      code: 200,
      message: "请求成功",
      data: "修改成功",
    };
  } else {
    return {
      code: 200,
      message: "请求成功",
      data: "添加成功",
    };
  }
});

Mock.mock("https://www.demo.com/tenementList", "post", (opt: any) => {
  return {
    code: 200,
    message: "请求成功",
    data: tenementList,
  };
});

//获取房间列表的接口
function generateRooms() {
  const rooms = [];
  for (let i = 0; i < 50; i++) {
    const floor = 1 + Math.floor(i / 6); // 每6个房间一层
    const roomNumber = floor * 100 + (101 + (i % 6)); // 计算房间号
    rooms.push({
      roomNumber,
      decorationType: Mock.Random.pick(["毛坯", "精装"]),
      area: Mock.Random.integer(70, 300),
      unitPrice: Mock.Random.integer(1, 3),
      src: "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png",
    });
  }
  return rooms;
}

Mock.mock("https://www.demo.com/roomList", "post", (options: any) => {
  // console.log("收到房间id", JSON.parse(options.body).roomid);
  return {
    code: 200,
    message: "成功",
    data: {
      rooms: generateRooms(),
    },
  };
});

//合同管理
Mock.mock("https://www.demo.com/contractList", "post", (options: any) => {
  const { pageNo, pageSize } = JSON.parse(options.body);
  console.log("后端合同管理接到参数", JSON.parse(options.body));
  return {
    code: 200,
    message: "成功",
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          contractNo: '@string("number", 6)',
          "type|1": ["租赁合同", "自定义合同", "购买合同"],
          "name|1": [
            "房屋租赁合同通用模版",
            "车位租赁合同通用模版",
            "商业房产买卖合同",
          ],
          "startDate|1": ["2023-01-01", "2023-03-05", "2023-04-01"],
          "endDate|1": ["2024-01-01", "2024-03-05", "2024-04-01"],
          "partyA|1": ["万物科技有限公司", "大鱼网络科技", "六六信息技术有限公司"],
          partyB: "天明物业有限公司",
          "status|1": ["1", "2", "3"],
        },
      ],
      total: 54,
      pageNo,
      pageSize,
    }),
    // 生成55条数据
  };
});
